# -*- coding: UTF-8 -*-

# Copyright (C) 2009 Daniele Favara.
# Written by Daniele Favara <daniele@zeroisp.com>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


import os, sys
import simplejson, urllib
from misc import unicode_urlencode

"""
http://www.panoramio.com/api/
- http://www.panoramio.com/map/get_panoramas.php?order=popularity&set=public&from=0&to=20&minx=-180&miny=-90&maxx=180&maxy=90&size=medium
"""

class PanoramioCom(object):
    #apy_key=u'ABQIAAAATw59ltFDL-sx11P8_WP6NRQNZk9Iyf_aiMB_Ni0yIfD-UN3HZxR7uFcLuIhccLGY4eXtbrUeQggL_w'

    def __init__(self, url="http://www.panoramio.com/map/get_panoramas.php"):
        """
        """
        self.url = url

    def _get(self, url):
        req = urllib.urlopen(url)
        print url
        ret = simplejson.load(req)
        return ret


    def search(self, lng,lat, **kw):
        params = {}
        params['order'] = kw.get('order', 'popularity')
        params['set'] = kw.get('set', 'public')
        params['size'] = kw.get('size', 'medium')
        params['from'] = kw.get('from', '0')
        params['to'] = kw.get('from', '10')
        epsilon = float(0.1)
        params['minx']=float(lng)-epsilon
        params['maxx']=float(lng)+epsilon
        params['miny']=float(lat)-epsilon
        params['maxy']=float(lat)+epsilon
        url = "%s?%s"%(self.url,unicode_urlencode(params))
        return self._get(url)

if __name__ == '__main__':
    p = PanoramioCom()
    for  i in p.search(44.4070624, 8.9339889):
        print i
